package com.ll.SchoolSafe.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ll.SchoolSafe.Service.SafetyService;
import com.ll.SchoolSafe.Service.SafetyThemeService;
import com.ll.SchoolSafe.pojo.CommonResult;
import com.ll.SchoolSafe.pojo.Safety;
import com.ll.SchoolSafe.pojo.SafetyTheme;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "安全教育")
public class SafeEducationController {
    @Autowired
    private SafetyService safetyService;
    @Autowired
    private SafetyThemeService safetyThemeService;
    /**
     * 获取栏目分类下的所有安全教育信息
     * @return
     */
    @ApiOperation("获取所有栏目")
    @GetMapping("/getSafeEducation/getAllSafetyTheme")
    public CommonResult getSafeEducation() {
        List<SafetyTheme> list = safetyThemeService.list();
        return new CommonResult(200,"获取所有栏目成功",list);
    }

    @ApiOperation("获取栏目下的所有安全教育信息按时间排序")
    @GetMapping("/getSafeEducation/getAllSafetyByThemeId/{safetyThemeId}")
    public CommonResult getSafetyTheme(@PathVariable Integer safetyThemeId) {
        List<Safety> list = safetyService.list(
                new QueryWrapper<Safety>()
                        .orderByDesc("release_time")
                        .eq("safety_theme_id", safetyThemeId)
        );
        return new CommonResult(200,"获取该栏目下的信息成功",list);
    }


    @ApiOperation("根据安全教育文章的id获取该文章的详细数据")
    @GetMapping("/getSafeEducation/getSafetyBySafetyId/{safetyId}")
    public CommonResult getAllSafety(@PathVariable Integer safetyId) {
        Safety sa = safetyService.getOne(new QueryWrapper<Safety>().eq("safety_id", safetyId));
        addReadCount(sa.getSafetyId());
        return new CommonResult(200,"获取所有信息成功",sa);
    }

    @ApiOperation("阅读文章时增加阅读量")
    @PutMapping("/getSafeEducation/addReadNumber/{safetyId}")
    public CommonResult addReadCount(@PathVariable Integer safetyId) {
        safetyService.addReadCount(safetyId);
        return new CommonResult(200,"阅读量增加成功",null);
    }
}
